package com.whut.whw.post;

import com.whut.whw.bean.City;
import com.whut.whw.bean.Province;
import com.whut.whw.util.LogUtils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by 张芳容 on 2017/06/13.
 */

public class ReadXML {

    public static ArrayList<Province> ReadProvinces() {
        ArrayList<Province> provinces = new ArrayList<Province>();
        try {
            //读取XML文件存放在Arraylist
            SAXReader reader = new SAXReader();
           // String path=com.whut.whw.util.Utils.getContext().getClass().getClassLoader().getResourceAsStream("assets/Provinces.xml");
           // File file = new File(path);
            InputStream in = com.whut.whw.util.Utils.getContext().getResources().getAssets().open("Provinces.xml");
            SAXReader sr = new SAXReader();// 获取读取xml的对象。
            Document document = sr.read(in);
          //  Document document = null;
          //  document = reader.read(file);
            Element root = document.getRootElement();
            List<Element> childElements = root.elements();
           // LogUtils.e("打印XML", String.valueOf(childElements.size()));
            for (Element child : childElements) {
               // LogUtils.e("打印", "ID: " + child.attributeValue("ID"));
                //LogUtils.e("打印", "ProvinceName" + child.attributeValue("ProvinceName"));
                Province province = new Province();
                province.setID(child.attributeValue("ID"));
                province.setProvinceName(child.attributeValue("ProvinceName"));
                provinces.add(province);
            }
        } catch (DocumentException e) {
            LogUtils.e("打印XML", e.toString());
            e.printStackTrace();
        } catch (Exception e1) {
            LogUtils.e("打印XML", e1.toString());
            e1.printStackTrace();
        }
        return provinces;
    }

    public static ArrayList<ArrayList<City>> ReadCities(ArrayList<Province> provinces) {
        ArrayList<ArrayList<City>> Cities=new ArrayList<ArrayList<City>>();
        try {
            //读取XML文件存放在Arraylist
            SAXReader reader = new SAXReader();
            InputStream in = com.whut.whw.util.Utils.getContext().getResources().getAssets().open("Cities.xml");
            SAXReader sr = new SAXReader();// 获取读取xml的对象。
            Document document = sr.read(in);
            Element root = document.getRootElement();
            List<Element> childElements = root.elements();
            LogUtils.e("打印XML", String.valueOf(childElements.size()));
            for(int i=0;i<provinces.size();i++){
                ArrayList<City> cityArrayList=new ArrayList<City>();
                for (Element child : childElements) {
                    City city=new City();
                    if( child.attributeValue("PID").equals(provinces.get(i).getID())){
                        city.setID(child.attributeValue("ID"));
                        city.setCityName( child.attributeValue("CityName"));
                        city.setPID(child.attributeValue("PID"));
                        city.setZipCode(child.attributeValue("ZipCode"));
                        cityArrayList.add(city);
                    }
                }
                LogUtils.e("打印获取城市", String.valueOf(cityArrayList.size()));
                Cities.add(cityArrayList);
            }
        } catch (DocumentException e) {
            LogUtils.e("打印XML", e.toString());
            e.printStackTrace();
        } catch (Exception e1) {
            LogUtils.e("打印XML", e1.toString());
            e1.printStackTrace();
        }
        return Cities;
    }
}